/*
Reversi.
Copyright (C) <2008>  <Vladimir Simo, Kristian Valentin, Michal Vrabel, Tibor Balazs>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once

//#include "stdio.h"
#include "hrac.h"

class CutOffHrac:public Hrac {
private:
	void setGameField(typPole &pole1);
	int evaulate(typPole& pole1,int x,int y, int color);
	Coordinates *getBestMove();
public:
	CutOffHrac(int color);
	void move(typPole &pole1, int x = 0, int y = 0);
        bool checkMove(typPole &pole1, int x, int y) {return true;};
        void getHelpMoves(TMemo *memo, typPole& pole1, int color) {};
	int getNumOfPossibleMoves(typPole& pole1, int color);
};

// ======================================
CutOffHrac::CutOffHrac(int color):Hrac() {
	setColor(color);
}

void CutOffHrac::setGameField(typPole &pole1) {
	for (int i=0;i<rozmerPola;i++){
		for (int j=0;j<rozmerPola;j++){
			pole[i][j] = pole1[i][j];
			//kopirovanie aj do pola roota, bez toho mi to nejde
			root->pole[i][j] = pole1[i][j];
		}
	}
}

int CutOffHrac::getNumOfPossibleMoves(typPole& pole1, int color) {
	Coordinates *tmp = getPossibleMoves(pole1, color);
	int counter = 0;
	if (tmp!=NULL){
		while(tmp->getNext() != NULL) {
			tmp = tmp->getNext();
			counter ++;
		}
	}
        if (tmp != NULL) {
                tmp->freeCoordinates();
                delete tmp;
        }

	return counter;
}

int CutOffHrac::evaulate(typPole& pole1,int x,int y, int color) {
	int heruistic = getNumOfMyPieces(pole1, color) + vratPocetObratenych(pole1, x, y, color) + getNumOfPossibleMoves(pole1, color);
	return heruistic;
}

Coordinates *CutOffHrac::getBestMove() {
	// vytvori strom a ohodnoti terminalne stavy
	buildTree(getPossibleMoves(pole, getColor()), root, 0, getColor());
	// ohodnoti zvysok stromu
	evaulateTree(root, getColor());
	//vypis stromu
	//printTree(root);
	
	// vyber si najlepsi tah a vrat jej Coordinates
	return chooseBestOption();
}

void CutOffHrac::move(typPole &pole1, int x, int y) {
	setGameField(pole1);
	// spravi najlepsi tah v aktualnom poli s vlastou farbou
	makeMove(getBestMove(), pole1, getColor());
	//destruuj strom
	destroyTree();
}
